import os
import json
import sys
import env
from typing import List
from alibabacloud_ecs20140526.client import Client as Ecs20140526Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_ecs20140526 import models as ecs_20140526_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient

class SecurityGroup:
    def __init__(self):
        pass

    @staticmethod
    def create_client() ->Ecs20140526Client:
        config = open_api_models.Config(
            access_key_id=env.AccessKey_ID,
            access_key_secret=env.AccessKey_Secret
        )
        config.endpoint = f'ecs.cn-beijing.aliyuncs.com'
        return Ecs20140526Client(config)

    @staticmethod
    def list_SecurityGroupItem():
        client = SecurityGroup.create_client()
        list = ecs_20140526_models.DescribeSecurityGroupAttributeRequest(
            region_id='cn-beijing',
            security_group_id=env.SourceGroup_Id,
            direction='ingress',
            nic_type='internet'
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            return client.describe_security_group_attribute_with_options(list, runtime).body.permissions.permission
        except Exception as error:
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    def add_SecurityGroupItem(ip_protocol,source_cidr_ip,port_range):
        client = SecurityGroup.create_client()
        permissions_0 = ecs_20140526_models.AuthorizeSecurityGroupRequestPermissions(
            policy='accept',
            ip_protocol=ip_protocol,
            source_cidr_ip=source_cidr_ip,
            source_group_id=env.SourceGroup_Id,
            port_range=port_range
        )
        authorize_security_group_request = ecs_20140526_models.AuthorizeSecurityGroupRequest(
            region_id='cn-beijing',
            security_group_id=env.SourceGroup_Id,
            nic_type='internet',
            permissions=[
                permissions_0
            ]
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            return  client.authorize_security_group_with_options(authorize_security_group_request, runtime)
        except Exception as error:
            # 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    def remove_SecurityGroupItem(security_group_rule_id):
        client = SecurityGroup.create_client()
        revoke_security_group_request = ecs_20140526_models.RevokeSecurityGroupRequest(
            region_id='cn-beijing',
            security_group_id=env.SourceGroup_Id,
            security_group_rule_id=[
                security_group_rule_id
            ]
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            return client.revoke_security_group_with_options(revoke_security_group_request, runtime)
        except Exception as error:
            # 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)

    @staticmethod
    def Change_SecurityGroupItem(security_group_rule_id,policy,ip_protocol,source_cidr_ip,port_range):
        client = SecurityGroup.create_client()
        modify_security_group_rule_request = ecs_20140526_models.ModifySecurityGroupRuleRequest(
            region_id='cn-beijing',
            security_group_id=env.SourceGroup_Id,
            security_group_rule_id=security_group_rule_id,
            policy=policy,
            priority='1',
            ip_protocol=ip_protocol,
            source_cidr_ip=source_cidr_ip,
            port_range=port_range
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.modify_security_group_rule_with_options(modify_security_group_rule_request, runtime)
        except Exception as error:
            # 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            # 诊断地址
            print(error.data.get("Recommend"))
            UtilClient.assert_as_string(error.message)